local prefabs =
{
    "collapse_small",
}

function MakeWallType(data)
    local assets =
    {
        Asset("ANIM", "anim/wall.zip"),
        Asset("ANIM", "anim/wall_" .. data.name .. ".zip"),
    }

    local bank = data.name == "bboy_nether_blossom" and "wall_bboy_nether_blossom" or data.name == "bboy_courtyard" and "wall_bboy_courtyard" or "wall"

    local onhit = data.material ~= nil and function(inst)
        inst.SoundEmitter:PlaySound("dontstarve/common/destroy_" .. data.material)
    end or nil

    local function onhammered(inst, worker)
        if data.maxloots ~= nil and data.loot ~= nil then
            local num_loots = 1
            for i = 1, num_loots do
                inst.components.lootdropper:SpawnLootPrefab(data.loot)
            end
        end

        local fx = SpawnPrefab("collapse_small")
        fx.Transform:SetPosition(inst.Transform:GetWorldPosition())
        if data.material ~= nil then
            fx:SetMaterial(data.material)
        end

        inst:Remove()
    end

    local function fn()
        local inst = CreateEntity()

        inst.entity:AddTransform()
        inst.entity:AddAnimState()
        inst.entity:AddSoundEmitter()
        inst.entity:AddNetwork()

        inst.Transform:SetEightFaced()

        inst:AddTag("wall")

        for k, v in ipairs(data.tags) do
            inst:AddTag(v)
        end

        inst.AnimState:SetBank(bank)
        inst.AnimState:SetBuild("wall_" .. data.name)
        inst.AnimState:PlayAnimation("broken", false)

        -- 潜入原始状态进行优化
        inst:AddTag("_named")

        inst.entity:SetPristine()

        if not TheWorld.ismastersim then
            return inst
        end

        -- 删除这些标签，以便在复制以下组件时可以正确添加它们
        inst:RemoveTag("_named")

        inst:AddComponent("inspectable")
        inst.components.inspectable.nameoverride = "wall_" .. data.name

        inst:AddComponent("lootdropper")

        inst:AddComponent("named")
        inst.components.named:SetName(STRINGS.NAMES["WALL_BBOY_NETHER_BLOSSOM"])

        inst:AddComponent("workable")
        inst.components.workable:SetWorkAction(ACTIONS.HAMMER)
        inst.components.workable:SetWorkLeft(3)
        inst.components.workable:SetOnFinishCallback(onhammered)
        inst.components.workable:SetOnWorkCallback(onhit)

        return inst
    end

    return Prefab("brokenwall_" .. data.name, fn, assets, prefabs)
end

local walldata =
{
    { name = "bboy_courtyard",      material = "stone", tags = { "stone" }, maxloots = 2, maxhealth = TUNING.STONEWALL_HEALTH },
    { name = "bboy_nether_blossom", material = "stone", tags = { "stone" }, maxloots = 2, maxhealth = TUNING.STONEWALL_HEALTH },
}

local wallprefabs = {}
for i, v in ipairs(walldata) do
    table.insert(wallprefabs, MakeWallType(v))
end
return unpack(wallprefabs)
